package com.xianggu.designMode.iterationMethod.demo1;

import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * @author chengjy
 * @date 2025/3/19
 */
public class UserRead implements Iterable<User>{

    List<String> texts;

    public UserRead(List<String> texts) {
        this.texts = texts;
    }

    @Override
    public Iterator<User> iterator() {
        return new UserIterator();
    }

    class UserIterator implements Iterator<User>{

        int index = 0;
        int size = texts.size();

        @Override
        public boolean hasNext() {
            return index < size;
        }

        @Override
        public User next() {
            if (hasNext()){
                String text = texts.get(index++);
                String[] split = text.substring(1, text.length() - 1).split(",");
                return new User(split[0], Integer.parseInt(split[1]));
            }
            throw new NoSuchElementException("没有下一个元素了");
        }
    }
}
